@use 'sass:map';

@use './shared' as *;
@use './design' as *;

$drawer: () !default;
$drawer: map.merge(
  (
    z-index: value('z-index-masker'),
    bg-color: value('bg-color-base'),
    s-color: value('shadow-color-base'),
    header-v-padding: 10px,
    header-h-padding: 14px,
    body-v-padding: 14px,
    body-h-padding: 16px,
    title-color: value('content-color-primary'),
    d-color: value('border-color-light-2'),
    title-font-size: value('font-size-primary'),
    close-color: value('content-color-placeholder'),
    close-color-hover: value('content-color-base'),
    title-weight: 500,
    handler-bg-color-hover: rgba($color-black, 0.15),
    handler-width: 5px,
    footer-v-padding: 10px,
    footer-h-padding: 16px,
  ),
  $drawer
);

.#{$namespace}-drawer {
  &-vars {
    @include define-preset-values('drawer', $drawer);
  }

  @include basis {
    z-index: value('drawer-z-index');
  }

  @include fixed;
  @include inherit-color;

  &--inner {
    position: absolute;
    overflow: hidden;
  }

  &__wrapper {
    position: absolute;
    display: flex;
    flex-direction: column;
    background-color: value('drawer-bg-color');
    border: 0;
    transform: translateZ(0);

    &--top,
    &--bottom {
      right: 0;
      left: 0;
      min-height: 60px;
      max-height: calc(100vh - 14px);
    }

    &--right,
    &--left {
      top: 0;
      bottom: 0;
      max-width: calc(100vw - 14px);
    }

    $hide-mask: #{&}--hide-mask;
    $shadow: value('shadow-blur') value('drawer-s-color');

    &--top {
      top: 0;

      &#{$hide-mask} {
        box-shadow: 0 2px $shadow;
      }
    }

    &--right {
      right: 0;

      &#{$hide-mask} {
        box-shadow: -2px 0 $shadow;
      }
    }

    &--bottom {
      bottom: 0;

      &#{$hide-mask} {
        box-shadow: 0 -2px $shadow;
      }
    }

    &--left {
      left: 0;

      &#{$hide-mask} {
        box-shadow: 2px 0 $shadow;
      }
    }

    &--resizing {
      user-select: none;
    }
  }

  &--inner &__wrapper {
    position: absolute;
  }

  &__header {
    position: relative;
    display: flex;
    flex-shrink: 0;
    align-items: center;
    padding: value('drawer-header-v-padding') value('drawer-header-h-padding');
    border-bottom: value('border-shape') value('drawer-d-color');
  }

  &--undivided &__header {
    border-bottom: 0;
  }

  &__title {
    flex: 1 0 0;
    font-size: value('drawer-title-font-size');
    font-weight: value('drawer-title-weight');
    color: value('drawer-title-color');
  }

  &__content {
    position: relative;
    flex: auto;
    min-width: 0;
    min-height: 0;
    padding: value('drawer-body-v-padding') value('drawer-body-h-padding');
  }

  &__footer {
    position: relative;
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: flex-end;
    padding: value('drawer-footer-v-padding') value('drawer-footer-h-padding');
    border-top: value('border-shape') value('drawer-d-color');
  }

  &--undivided &__footer {
    border-top: 0;
  }

  &__close {
    display: flex;
    align-items: center;
    height: 24px;
    padding: 0;
    margin-inline-start: 6px;
    color: value('drawer-close-color');
    cursor: pointer;
    background-color: transparent;
    border: 0;
    outline: 0;
    transition: value('transition-color');

    &:hover,
    &:focus {
      color: value('drawer-close-color-hover');
    }
  }

  &__handler {
    $offset: calc(value('drawer-handler-width') * -0.5 + 0.5px);

    position: absolute;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: value('transition-background');

    &--top,
    &--bottom {
      right: 0;
      left: 0;
      height: value('drawer-handler-width');
      cursor: ns-resize;
    }

    &--right,
    &--left {
      top: 0;
      bottom: 0;
      width: value('drawer-handler-width');
      cursor: ew-resize;
    }

    &--top {
      bottom: $offset;
    }

    &--right {
      left: $offset;
    }

    &--bottom {
      top: $offset;
    }

    &--left {
      right: $offset;
    }

    &:hover,
    &--resizing {
      background-color: value('drawer-handler-bg-color-hover');
    }
  }
}
